﻿using _1_31_2015_SLMSite.Data;
using _1_31_2015_SLMSite.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace _1_31_2015_SLMSite.Controllers
{
    public class AppointmentController : Controller
    {
        public List<ApptTypeViewModel> HydrateAppTypes()
        {
            List<ApptTypeViewModel> model = null;
            using(ApplicationDbContext db = new ApplicationDbContext())
            {
                model = db.ApptTypes.Select(a => new ApptTypeViewModel
                {
                    ApptTypeDesc = a.ApptTypeDesc,
                    ApptTypeId = a.ApptTypeId
                }).ToList();
            }
            if (model == null)
            {
                model = new List<ApptTypeViewModel>();
            }
            return model;
        }

        public List<ApptTimeFrameViewModel> HydrateApptTimeFrames()
        {
            List<ApptTimeFrameViewModel> model = null;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                model = db.ApptTimeFrames.Select(a => new ApptTimeFrameViewModel
                {
                    ApptTimeFrameDesc = a.ApptTimeFrameDesc,
                    ApptTimeFrameId = a.ApptTimeFrameId
                }).ToList();
            }
            if (model == null)
            {
                model = new List<ApptTimeFrameViewModel>();
            }
            return model;
        }

        /* -------------------------------------------------------------------------------------------- */

        // GET: Appointment
        public ActionResult Create()
        {
            AppointmentViewModel model = new AppointmentViewModel();
            model.ApptTypes = HydrateAppTypes();
            model.ApptTimeFrames = HydrateApptTimeFrames();
            model.Date = DateTime.Now;
            model.DateString = DateTime.Now.ToString();
            return View(model);
        }

        [HttpPost]
        public ActionResult Create(AppointmentViewModel model, string apptDate)
        {
           
            model.ApptTypes = HydrateAppTypes();
            model.ApptTimeFrames = HydrateApptTimeFrames();

            return View(model);
        }

    }
}